package com.haizhi.crm.api.controller.product;


import com.haizhi.crm.biz.model.ProductQo;
import com.haizhi.crm.biz.model.ProductVo;
import com.haizhi.crm.biz.service.ProductService;
import com.haizhi.crm.biz.service.VisitRecordService;
import com.haizhi.crm.common.constant.BizStatus;
import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.common.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * Desc:产品业务
 * Created by yangyijun on 2018/06/03.
 */
@Api(description = "[业务-产品]" )
@RestController
@RequestMapping("/product" )
public class ProductController {

    private static final GLog LOG = LogFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;
    @Autowired
    private VisitRecordService visitRecordService;

    @ApiOperation(value = "产品详情页-查询产品相关联的拜访记录", notes = "?productKey=DF3L54944ADEE991715A1D521C6903FF" )
    @GetMapping("/findAssociatedVisitRecords" )
    public Response findAssociatedVisitRecords(@RequestParam String productKey) {
        return visitRecordService.findAssociatedVisitRecords(productKey, CrmSchema.PRODUCT);
    }

    @ApiOperation(value = "产品详情-查询产品信息", notes = "根据产品object_key查询ES的tv_product索引" )
    @GetMapping("/detail" )
    public Response detail(@RequestParam String productKey) {
        return productService.detail(productKey);
    }


    @ApiOperation(value = "产品详情-查询客户", notes = "根据产品object_key查询ES的account" )
    @GetMapping("/customer" )
    public Response customer(@RequestParam String productKey) {
        return productService.findProduceCustomer(productKey);
    }


    @ApiOperation(value = "查询产品销售记录(产品详情页)", notes = "根据产品object_key查询ES的product_service_sales索引" )
    @GetMapping("/querySalesList" )
    public Response querySalesList(@RequestParam @NotNull String productKey) {
        try {

            if (StringUtils.isNotBlank(productKey)) {
                ProductVo productDo = productService.querySalesList(productKey);
                return Response.success(productDo);
            } else {
                return Response.error().setErrorInfo(BizStatus.PRODUCT_APIPARAM_NULL);
            }

        } catch (Exception e) {
            LOG.error(BizStatus.PRODUCT_QUERY_SALES_ERROR.getDesc(), e);
            return Response.error().setErrorInfo(BizStatus.PRODUCT_QUERY_SALES_ERROR);
        }
    }

    @ApiOperation(value = "产品列表-从es查询产品列表", notes = "{\"type\":\"投资\", \"pageNo\": 1, \"pageSize\": 10}" )
    @PostMapping("/search" )
    public Response search(@RequestBody @Valid ProductQo productQo) {
        return productService.searchProducts(productQo);
    }
}
